package main

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
	"time"
)

// 元素为map类型的切片
func mapSlice() {
	var mapSlice = make([]map[string]string, 3)
	for index, value := range mapSlice {
		fmt.Printf("index:%d value:%v\n", index, value)
	}
	fmt.Println("after init")
	// 对切片中的map元素进行初始化
	mapSlice[0] = make(map[string]string, 10)
	mapSlice[0]["name"] = "小王子"
	mapSlice[0]["password"] = "123456"
	mapSlice[0]["address"] = "沙河"
	for index, value := range mapSlice {
		fmt.Printf("index:%d value:%v\n", index, value)
	}
}

// 值为切片类型的map
func sliceMap() {
	var sliceMap = make(map[string][]string, 3)
	fmt.Println(sliceMap)
	fmt.Println("after init")
	key := "中国"
	value, ok := sliceMap[key]
	if !ok {
		value = make([]string, 0, 2)
	}
	value = append(value, "北京", "上海")
	sliceMap[key] = value
	fmt.Println(sliceMap)
}

/**
* Golang中 map的key可以用数组array类型吗？map的key可以用切片slice类型吗？为什么slice、map、function不能直接用==比较？
* https://blog.csdn.net/m0_57116438/article/details/122337061
 */
type user struct {
	love    *string
	Addr    string
	Country string
}

func doRedis() {
	client := redis.NewClient(&redis.Options{
		Addr:         "localhost:6379",
		Password:     "", // no password set
		DB:           0,  // use default DB
		WriteTimeout: 10 * time.Second,
		ReadTimeout:  10 * time.Second,
	})

	ctx := context.Background()

	err := client.Set(ctx, "good", "bar", 0).Err()
	if err != nil {
		panic(err)
	}

	val, err := client.Get(ctx, "good").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println("good", val)

	fmt.Println("--------------------")
	session := map[string]string{"name": "John", "surname": "Smith", "company": "Redis", "age": "29"}
	for k, v := range session {
		err := client.HSet(ctx, "user-session:123", k, v).Err()
		if err != nil {
			panic(err)
		}
	}

	userSession := client.HGetAll(ctx, "user-session:123").Val()
	fmt.Println(userSession)
}

func main() {
	sliceMap()
	fmt.Printf("---------------------")
	mapSlice()
	fmt.Printf("------------")

	//hits := make(map[user]int)
	//var a []int
	//var b []int
	//fmt.Println(a == b)
	//
	//m_1 := map[int]int{}
	//m_2 := map[int]int{}
	//if m_1 == m_2 {
	//	fmt.Println("equal")
	//
	//}
	doRedis()
}
